package com.lele.rabbit;

import com.lele.constant.MailContant;
import com.lele.pojo.Employee;
import com.lele.utils.Result;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Component

public class MailReceive {


    @Resource
    private JavaMailSender mailSender;
    @Resource
    private MailProperties mailProperties;
    @Resource
    private TemplateEngine templateEngine;
    @Resource
    private RedisTemplate redisTemplate;



    @RabbitListener(queues = MailContant.MAIL_QUEUE)
    public void handler(Message message, Channel channel) throws MessagingException, IOException {
       /* MimeMessage mainMessage = mailSender.createMimeMessage();
        //用MimeMessageHelper组装复杂邮件，第二个参数为true，可以发送附件
        MimeMessageHelper helper = new MimeMessageHelper(mainMessage, true);
        //发送者邮箱
        helper.setFrom("leleupup@qq.com");
        //接收者邮箱
        helper.setTo("leleupup@qq.com");
        //发送的邮件标题
        helper.setSubject("Springboot发送邮件");
        //发送的内容
        helper.setText("邮件内容");
        //添加附件
        ClassPathResource classPathResource = new ClassPathResource("pdf/v4.0-JavaGuide面试突击版.pdf");
        String path = classPathResource.getFile().getPath();
        helper.addAttachment("javaguide.pdf", new File(path));
        try {
            mailSender.send(mainMessage);
        } catch (MailException e) {
            e.printStackTrace();
        }*/
        Employee employee = (Employee) message.getPayload();
        MessageHeaders headers = message.getHeaders();
        long tagId = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        String msgId= (String) headers.get("spring_returned_message_correlation");
        MimeMessage mainMessage = mailSender.createMimeMessage();
        //用MimeMessageHelper组装复杂邮件，第二个参数为true，可以发送附件
        MimeMessageHelper helper = new MimeMessageHelper(mainMessage, true);
        //使用lua脚本保证原子性
        BoundHashOperations boundHashOps = redisTemplate.boundHashOps("mail-send");
        if(boundHashOps.hasKey(msgId)){
            //表明已经被消费过了
            /**
             * tagId:消息序号
             * multiple：是否可以确认多条
             *
             */
            channel.basicAck(tagId,false);
            return;
        }

        helper.setFrom("leleupup@qq.com");
        helper.setTo(employee.getEmail());
        helper.setSubject("入职邮件");
        helper.setSentDate(new Date());
        Context context = new Context();
        context.setVariable("name", employee.getName());
        String mail = templateEngine.process("mail", context);
        //支持发送html
        helper.setText(mail, true);
        try {
            mailSender.send(mainMessage);
            //存入redis中
            boundHashOps.put(msgId,"yes");
            channel.basicAck(tagId,false);
        } catch (MailException e) {
            e.printStackTrace();
            //重新发送，将消息回到队列中
            channel.basicNack(tagId,false,true);
        }
    }
}
